package com.example.module_car.ui.edit

import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.MultiAutoCompleteTextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.amap.api.maps.AMap
import com.amap.api.maps.MapView
import com.amap.api.maps.model.MyLocationStyle
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.utils.ToastUtils
import com.example.module_car.R
import com.example.module_car.adapter.CarAdapter
import com.example.module_car.model.AddCarEntity
import com.example.module_car.model.AddCarParam
import com.example.module_car.ui.CarViewModel
import com.example.module_car.ui.addcar.AddCarViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class EditActivity : AppCompatActivity() {
    private val mViewModel : AddCarViewModel by viewModels()
    private val mViewModelQuery : CarViewModel by viewModels()
    private lateinit var carAdapter : CarAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_edit)
        val etUser = findViewById<EditText>(R.id.etUser)
        val etPlate = findViewById<EditText>(R.id.etPlate)
        val btn = findViewById<Button>(R.id.btnSave)

        val carItem = intent.getSerializableExtra("car_item") as AddCarEntity
        etUser.setText(carItem.ownerName)
        etPlate.setText(carItem.licensePlate)

        initSizeMap(savedInstanceState)
        showDownAdd()
        showDownCarMore()

        btn.setOnClickListener {
            val user = etUser.text.toString()
            val plate = etPlate.text.toString()
            val param = AddCarParam(
                licensePlate = plate,
                ownerName = user,
                provinceAbbreviation = carItem.provinceAbbreviation,
                userId = MMKV.defaultMMKV().decodeInt("userId"),
            )
            mViewModel.upDateCar(carItem.id,param)
            mViewModel.mUpDateCar.observeWithLifecycle(this){
                when(it){
                    Empty -> {}
                    is Failed -> {
                        ToastUtils.showShort(this,"修改失败"+it.message)
                    }
                    Loading -> {}
                    Success -> {
                        carAdapter = CarAdapter()
                        mViewModelQuery.getQueryCarList(carItem.id)
                        mViewModelQuery.mCarList.observe(this){
                            carAdapter.addListItem(it)
                        }
                        ToastUtils.showShort(this,"修改成功")
                        onBackPressed()
                    }
                }
            }
        }
    }

    private fun showDownAdd(){
        val items = listOf("A 区地下停车场", "B 区地下停车场", "C 区地下停车场")
        val adapter = ArrayAdapter(this, R.layout.dropdown_item, items)
        val car_add_more = findViewById<MultiAutoCompleteTextView>(R.id.car_add_more)
        car_add_more.setTokenizer(MultiAutoCompleteTextView.CommaTokenizer())
        car_add_more.setAdapter(adapter)

        car_add_more.setOnItemClickListener { _, _, position, _ ->
            car_add_more.setText(items[position])
        }
    }
    private fun showDownCarMore(){
        val items = listOf("普通车位", "高价车位", "贵族车位")
        val adapter = ArrayAdapter(this, R.layout.dropdown_item, items)

        val car_more = findViewById<MultiAutoCompleteTextView>(R.id.car_more)
        car_more.setTokenizer(MultiAutoCompleteTextView.CommaTokenizer())
        car_more.setAdapter(adapter)

        car_more.setOnItemClickListener { _, _, position, _ ->
            car_more.setText(items[position])
        }
    }
    fun gotoBack(view: View){
        onBackPressed()
    }

    fun initSizeMap(savedInstanceState: Bundle?) {
        val mMapView =  findViewById<MapView>(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState)
        val aMap: AMap = mMapView.getMap()

        val myLocationStyle = MyLocationStyle() //初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000) //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMap.myLocationStyle = myLocationStyle //设置定位蓝点的Style

        //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
        aMap.isMyLocationEnabled = true // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
    }
}